package com.roadmap.ajax.handler;

import org.apache.log4j.Logger;

import com.roadmap.ajax.proxy.BaseProxy;
import com.roadmap.biz.common.IManager;
import com.roadmap.biz.common.ManagerFactory;
import com.roadmap.common.RdcConst;
import com.roadmap.common.RdcErrorMessage;
import com.roadmap.common.RdcLogger;
import com.roadmap.entities.LifeGoal;
import com.roadmap.entities.LifePlan;
import com.roadmap.entities.LifeTask;

public class TaskHandler extends BaseProxy {
	private final static Logger logger = RdcLogger.getLogger(TaskHandler.class);

	public Object completeTask(String aspirantId, String taskId) {
		if (taskId == null) {
			return RdcErrorMessage.BAD_PARAMETERS;
		}

		try {
			return complete(RdcConst.TASK, taskId);
		}
		catch (Exception e) {
			return RdcErrorMessage.getMessageToClient(e);
		}
	}

	// public LifeTask[] getAllTasks(String aspirantId, Date first, Date last) {
	// if (aspirantId == null) {
	// return null;
	// }
	// try {
	// List<BaseEntity> list = getTaskManager(aspirantId)
	// .getEntitiesInDateScope(first, last);
	// if (list != null) {
	// return list.toArray(new LifeTask[list.size()]);
	// }
	// }
	// catch (Exception e) {
	// logger.error("failed to return tasks for " + aspirantId, e);
	// }
	//
	// return null;
	// }

	public Object saveTask(String aspirantId, LifeTask task) {
		if (task == null) {
			return RdcErrorMessage.BAD_PARAMETERS;
		}
		try {
			return (task.getId() == null) ? create(task) : update(task);
		}
		catch (Exception e) {
			return RdcErrorMessage.getMessageToClient(e);
		}
	}

	public Object deleteTask(String aspirantId, String taskId) {
		if (taskId == null) {
			return RdcErrorMessage.BAD_PARAMETERS;
		}

		try {
			return delete(RdcConst.TASK, taskId);
		}
		catch (Exception e) {
			return RdcErrorMessage.getMessageToClient(e);
		}
	}

	public LifeGoal getGoal(String aspirantId, String goalId) {
		if (aspirantId == null || goalId == null) {
			return null;
		}

		try {
			IManager mgr = ManagerFactory
					.getInstance(RdcConst.GOAL, aspirantId);
			return (LifeGoal) mgr.get(goalId);
		}
		catch (Exception e) {
			logger.error("failed to get the plan for " + aspirantId, e);
		}
		return null;
	}

	public Object cancelTask(String aspirantId, String taskId) {
		try {
			return cancel(RdcConst.TASK, taskId);
		}
		catch (Exception e) {
			return RdcErrorMessage.getMessageToClient(e);
		}
	}
}
